package com.spark.incoicings.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spark.incoicings.dao.CustomerMapper;
import com.spark.incoicings.dao.DeliveryOrderMapper;
import com.spark.incoicings.service.CustomerService;
import com.spark.incoicings.service.DeliveryOrderService;
import com.spark.incoicings.service.DictTypeService;
import com.spark.incoicings.vo.CustomerVO;
import com.spark.incoicings.vo.DictVO;
import com.spark.invoicings.entity.DeliveryOrder;
import com.spark.invoicings.utils.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class DeliveryOrderServiceImpl extends
        ServiceImpl<DeliveryOrderMapper, DeliveryOrder> implements DeliveryOrderService {

    @Resource
    private DeliveryOrderMapper deliveryOrderMapper;
    @Resource
    private DictTypeService dictTypeService;
    @Resource
    private CustomerMapper customerMapper;
    @Override
    public List<DeliveryOrder> selectDeliveryOrderAll(Map<String, Object> map) {
        List<DeliveryOrder> deliveryOrders = deliveryOrderMapper.selectDeliveryOrderAll(map);
        deliveryOrders.forEach(deliveryOrder -> {
            CustomerVO customerVO = customerMapper.selectCustomerVo(deliveryOrder.getCustomer().getCustomerId());
            deliveryOrder.getCustomer().setBillingMethod(customerVO.getBillingMethod());
            deliveryOrder.getCustomer().setCustomerSource(customerVO.getCustomerSource());
            deliveryOrder.getCustomer().setCustomerRating(customerVO.getCustomerRating());
            DictVO dictTow = dictTypeService.selectDictTow(deliveryOrder.getSalesOrder().getOrderId());
            deliveryOrder.getSalesOrder().setStatus(dictTow.getStatus());
            if (deliveryOrder.getOutboundOrderId()!=null){
                DictVO outboundType = dictTypeService.selectOutboundType(deliveryOrder.getOutboundOrderId());
                deliveryOrder.getOutboundOrder().setStatus(outboundType.getStatus());
            }
            DictVO shipping = dictTypeService.selectShipping(deliveryOrder.getDeliveryId());
            deliveryOrder.setMethod(shipping.getStatus());
        });
        return deliveryOrders;
    }
}
